{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Control Structures & Iterables — Code Cards (Classroom Version)\n", "\n", "## Try me\n", "[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/ffraile/computer_science_tutorials/blob/main/source/Introduction/exercises/Basic%20control%20structure%20cards%20notebook.ipynb)[![Binder](https://mybinder.org/badge_logo.svg)](https://mybinder.org/v2/gh/ffraile/computer_science_tutorials/main?labpath=source%2FIntroduction%2Fexercises%2FBasic%20control%20structure%20cards%20notebook.ipynb)\n", "\n", "## How to use\n", "- Each card below mirrors the A4 activity prompt. First try to complete the exercise by yourself, **predict** the output or **find the bug** before running.\n", "- Then run the code cell to verify, or run the **Fix** cell (when provided) to see a correct version.\n", "- Keep explanations short and schematic (what/why).\n", "### Rendering Gemini into an interactive AI tutor\n", "- If you need help, hints, or extra exercises from AI, use the following prompt to convert it into an AI-tutor\n", "\n", "```markdown\n", "You are a **coding tutor** for Python in Jupyter/Colab. Follow the **course motto** “do not give up learning.”\n", "\n", "### Role & Goals\n", "- Use **Socratic guidance** and **test-first thinking** to help me solve problems myself.\n", "- Help me read errors, reason about state, and make small, safe iterations.\n", "\n", "### Strict Rules\n", "1) **Do not** provide full working solutions or paste complete functions/programs.\n", " - You may show **tiny illustrative fragments (≤3 lines)** or **pseudo-code with TODOs**, but not a drop-in answer.\n", "2) Prefer **questions over answers**; offer **one small next step** at a time.\n", "3) When debugging, explain **what the traceback says**, give **2–3 hypotheses**, and propose the **smallest diff** in *plain English* first.\n", "4) Encourage **TDD**: ask me to write/assert a test, predict, run, and report outputs.\n", "5) Keep responses concise (≈120–150 words) unless I ask for a deeper explanation or code review.\n", "6) Ask me to **run code and share results**; adapt based on the output.\n", "7) If I request the full solution, remind me of the rules and offer a **higher-tier hint** instead.\n", "8) When I finalize an exercise, reinforce learning lessons and suggest additional exercises\n", "\n", "### Interaction Loop (use this structure)\n", "- **Restate goal:** what I’m trying to accomplish in one line.\n", "- **Diagnose:** key assumption to check or error to interpret.\n", "- **Hint (tiered):**\n", " - Tier 1: Conceptual nudge (no code).\n", " - Tier 2: Directed hint (identify line/construct to change).\n", " - Tier 3: Pseudo-code with TODOs or a **1–3 line** pattern (still not a full solution).\n", "- **Next action:** one concrete step for me to try now.\n", "- **Ask back:** what to run/paste (output, test result, or traceback).\n", "\n", "### When reviewing my code\n", "- Comment on **correctness, clarity, naming, and complexity (big-O)**.\n", "- Suggest **tests** I’m missing (boundaries, empty cases, error paths).\n", "\n", "### Safety & Ethics\n", "- No secrets or private data in prompts.\n", "- avoid library functions/APIs unless I ask.\n", "\n", "Stay in tutor mode for the whole session.\n", "```" ], "id": "de6aea45f9d94a80" }, { "cell_type": "markdown", "metadata": {}, "source": "## Basic control structures - If-else statements\n", "id": "876e3c44bd5fe2c1" }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Card IF-1 — Predict the output\n", "\n", "```python\n", "x = 20\n", "if x < 20:\n", " msg = \"A\"\n", "elif x <= 20:\n", " msg = \"B\"\n", "else:\n", " msg = \"C\"\n", "print(msg)\n", "```\n", "**Question:** What is printed?" ], "id": "1a8fdba52a530f0f" }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Card IF-1 — run to check\n", "x = 20\n", "if x < 20:\n", " msg = \"A\"\n", "elif x <= 20:\n", " msg = \"B\"\n", "else:\n", " msg = \"C\"\n", "print(msg) # expected: B" ], "id": "f64745c08ce74548" }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Card IF-2 — Predict the output\n", "\n", "```python\n", "x = 19\n", "msg = \"X\"\n", "if x < 20:\n", " msg = \"A\"\n", "if x < 19:\n", " msg = \"B\"\n", "else:\n", " msg = \"C\"\n", "print(msg)\n", "```\n", "**Question:** What is printed?" ], "id": "835ca9c9101a2233" }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Card IF-2 — run to check\n", "x = 19\n", "msg = \"X\"\n", "if x < 20:\n", " msg = \"A\"\n", "if x < 19:\n", " msg = \"B\"\n", "else:\n", " msg = \"C\"\n", "print(msg) # expected: C (else binds to second if)" ], "id": "ba2a1872e27dcfee" }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Card IF-3 — Predict the output\n", "\n", "```python\n", "temp = 22\n", "if temp > 20:\n", " if temp < 25:\n", " res = \"A\"\n", "else:\n", " res = \"B\"\n", "print(res)\n", "```\n", "**Question:** What is printed?" ], "id": "ad38efddba7c69b" }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Card IF-3 — run to check\n", "temp = 22\n", "if temp > 20:\n", " if temp < 25:\n", " res = \"A\"\n", "else:\n", " res = \"B\"\n", "print(res) # expected: A (nested if is True; outer else is skipped entirely)" ], "id": "d6a26097a30c82df" }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Card IF-4 — Code Wizard (Optimize the branching)\n", "\n", "Original:\n", "```python\n", "temp = 22\n", "if temp > 20:\n", " if temp < 25:\n", " res = \"A\"\n", "else:\n", " res = \"B\"\n", "print(res)\n", "```\n", "**Task:** Rewrite with a clear `if/elif/else` chain." ], "id": "527385e6057d3b6f" }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Card IF-4 — one possible optimization\n", "temp = 22\n", "if temp <= 20:\n", " res = \"B\"\n", "elif temp < 25:\n", " res = \"A\"\n", "else:\n", " res = \"B\"\n", "print(res) # clearer mutually-exclusive branches" ], "id": "b9fa88de31b139dc" }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Card IF-5 — Predict the output\n", "\n", "```python\n", "score = 90\n", "if score >= 80:\n", " grade = \"A\"\n", "elif score >= 90:\n", " grade = \"A+\"\n", "else:\n", " grade = \"B\"\n", "print(grade)\n", "```\n", "**Question:** What is printed?" ], "id": "7895fc7cf1ff8d32" }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Card IF-5 — run to check\n", "score = 90\n", "if score >= 80:\n", " grade = \"A\"\n", "elif score >= 90:\n", " grade = \"A+\"\n", "else:\n", " grade = \"B\"\n", "print(grade) # expected: A (first true branch wins)" ], "id": "4de1ff3f3f0c4ab6" }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 2) while loops — Human Calculator & Code Detective" ], "id": "91837f5141ab491e" }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Card WH-1 — Code Detective (Find the bug)\n", "\n", "Buggy code (indentation error):\n", "```python\n", "i = 0\n", "while i < 5:\n", " i += 1\n", " print(i)\n", " if i % 2 == 0:\n", " continue\n", "```\n", "**Task:** Fix indentation so it runs and behaves as intended." ], "id": "e2210e2479a1e24c" }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Card WH-1 — Fixed version\n", "i = 0\n", "while i < 5:\n", " i += 1\n", " print(i)\n", " if i % 2 == 0:\n", " continue" ], "id": "519eee7cf723f69c" }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Card WH-2 — Predict the output\n", "\n", "```python\n", "i = 3\n", "while i > 0:\n", " print(i, end=\"\")\n", " i -= 1\n", "print(\"done\")\n", "```\n", "**Question:** What is printed?" ], "id": "ad2b1d606a96c900" }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Card WH-2 — run to check\n", "i = 3\n", "while i > 0:\n", " print(i, end=\"\")\n", " i -= 1\n", "print(\"done\") # expected: 321done" ], "id": "50f94579a935c106" }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Card WH-3 — Human Calculator\n", "\n", "```python\n", "n = 0\n", "while True:\n", " n += 1\n", " if n == 3:\n", " continue\n", "print(n)\n", "```\n", "**Question:** How many times does the loop run?" ], "id": "e954aae6aac64dc7" }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Card WH-4 — Predict the output\n", "\n", "```python\n", "i, s = 0, 0\n", "while i < 5:\n", " i += 1\n", " if i % 2 == 0:\n", " continue\n", " s += i\n", "print(s)\n", "```\n", "**Question:** What is printed?" ], "id": "71337b6e78da2827" }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Card WH-4 — run to check\n", "i, s = 0, 0\n", "while i < 5:\n", " i += 1\n", " if i % 2 == 0:\n", " continue\n", " s += i\n", "print(s)" ], "id": "463790749562facd" }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 3) for loops — Human Calculator & Code Detective" ], "id": "20df6437cf22bc9c" }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Card FOR-1 — Predict the output\n", "\n", "```python\n", "names = [\"A\",\"B\"]\n", "for i, n in enumerate(names, start=1):\n", " print(i, n)\n", "```\n", "**Question:** What is printed?" ], "id": "a3ba1526009df22a" }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Card FOR-1 — run to check\n", "names = [\"A\",\"B\"]\n", "for i, n in enumerate(names, start=1):\n", " print(i, n)" ], "id": "5b06e53555337f9b" }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Card FOR-2 — Predict the output\n", "\n", "```python\n", "for x in [0,1,2]:\n", " if x:\n", " continue\n", " print(x)\n", "```\n", "**Question:** What is printed?" ], "id": "6a20ce4c51d9dd55" }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Card FOR-2 — run to check\n", "for x in [0,1,2]:\n", " if x:\n", " continue\n", " print(x)" ], "id": "72f372e139e7f2c6" }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Card FOR-3 — Predict the output\n", "\n", "```python\n", "for i in range(5):\n", " print(2**i)\n", "```\n", "**Question:** What is printed?" ], "id": "ca542429412c08e4" }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Card FOR-3 — run to check\n", "for i in range(5):\n", " print(2**i)" ], "id": "425726ee849d622" }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Card FOR-4 — Code Detective (Find the bug)\n", "\n", "Prompt comment:\n", "```python\n", "# print odd numbers from 1 to 5 [1, 3, 5]\n", "for i in range(5, 1, 1):\n", " print(i)\n", "```\n", "**Task:** Fix the loop to match the comment." ], "id": "e34d86684d6a1020" }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Fix the loop to match the comment. Hint: check range arguments\n", "for i in range(5, 1, 1):\n", " print(i) # 1, 3, 5" ], "id": "cfc7d1c79ee73df5" }, { "cell_type": "markdown", "metadata": {}, "source": [ "---\n", "**Notes**\n", "- `if/elif/else`: first true branch executes; `else` binds to the nearest unmatched `if`.\n", "- `while True` without `break` never terminates.\n", "- `continue` skips to the next iteration; `break` exits the loop.\n", "- `enumerate(iterable, start=1)` yields `(index, item)`.\n", "- `range(start, stop, step)`: `stop` is **exclusive**." ], "id": "f7e9c1e47c462db9" }, { "metadata": {}, "cell_type": "markdown", "source": [ "## Extra exercises\n", "### Card EX-1 — Predict the output\n", "```python\n", "for i in range(3):\n", " for j in range(2):\n", " if i == j:\n", " print(i, j)\n", "```\n", "**Question:** What is printed?\n" ], "id": "b81c1f30843a1b85" }, { "metadata": {}, "cell_type": "code", "outputs": [], "execution_count": null, "source": [ "for i in range(3):\n", " for j in range(2):\n", " if i == j:\n", " print(i, j)" ], "id": "d7b0604121a5c2a" }, { "metadata": {}, "cell_type": "markdown", "source": [ "### Card EX-2 — Code Wizard (Optimize the loop)\n", "**Task:** Fix the code so that \"Done\" is printed when the loop completes, without unnecessary logic. Explain your changes.\n", "```python\n", "i = 0\n", "while i < 5:\n", " print(i)\n", " i += 1\n", " if i == 5:\n", " print(\"Done\")\n", " break\n", "```\n" ], "id": "b853095ee4f00126" }, { "metadata": {}, "cell_type": "code", "outputs": [], "execution_count": null, "source": [ "i = 0\n", "while i < 5:\n", " print(i)\n", " i += 1\n", " if i == 5:\n", " print(\"Done\")\n", " break" ], "id": "5378f21da3abd521" }, { "metadata": {}, "cell_type": "markdown", "source": [ "### Card EX-3 — Human Calculator (Predict the output)\n", "What is printed?\n", "```python\n", "s = 0\n", "for i in range(1, 6, 2):\n", " if i % 2 == 0:\n", " s += i\n", " else:\n", " s += i * 2\n", "print(s)\n", "```\n" ], "id": "aeb339a1a2eeec61" }, { "metadata": {}, "cell_type": "code", "source": [ "s = 0\n", "for i in range(1, 6, 2):\n", " if i % 2 == 0:\n", " s += i\n", " else:\n", " s += i * 2\n", "print(s)" ], "id": "2738ed32298f6540", "outputs": [], "execution_count": null }, { "metadata": {}, "cell_type": "markdown", "source": [ "## Card EX-4 — Human Calculator (Predict the output)\n", "What is printed?\n", "```python\n", "a = [1, 2, 3, 4, 5, 6, 7]\n", "print(a[::-2])\n", "```\n" ], "id": "e4df727252e02a4d" }, { "metadata": {}, "cell_type": "code", "outputs": [], "execution_count": null, "source": [ "a = [1, 2, 3, 4, 5, 6, 7]\n", "print(a[::-2])" ], "id": "b893f6baa4a67571" }, { "metadata": {}, "cell_type": "markdown", "source": [ "## Card EX-5 — Code Wizard (Fix slicing)\n", "**Task:** Fix the code to print [\"B\", \"o\", \"n\", \"e\"] using as much defaults as possible\n", "```python\n", "b = [\"B\", \"r\", \"o\", \"w\", \"n\", \"i\", \"e\"]\n", "print(b[1:7:2])\n", "```\n" ], "id": "830653c2dd43319c" }, { "metadata": { "ExecuteTime": { "end_time": "2025-10-18T11:37:10.755027Z", "start_time": "2025-10-18T11:37:10.752822Z" } }, "cell_type": "code", "source": [ "b = [\"B\", \"r\", \"o\", \"w\", \"n\", \"i\", \"e\"]\n", "print(b[1:7:2])" ], "id": "2ca236214ac092d4", "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['r', 'w', 'i']\n" ] } ], "execution_count": 5 } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "name": "python", "version": "3.x" } }, "nbformat": 4, "nbformat_minor": 5 }